package com.linjiahao.servicebase.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @ClassName AsyncTaskExecutorConfig
 * @Description: 异步线程配置
 * @Author linjiahao
 * @Date 7/1/2022
 **/
@Configuration
public class AsyncTaskExecutorConfig {

    /**
     * 创建异步线程池
     * @return
     */
    @Bean("MessageTaskExecutor")
    public Executor creatThreadPool1(){
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(1); //初始化线程数
        executor.setMaxPoolSize(10);   //最大线程数
        executor.setQueueCapacity(100);   //队列数量
        executor.setThreadNamePrefix("");   //线程名称前缀
        executor.setKeepAliveSeconds(4); //空闲现成的存在时间 秒数
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());    //超过队列数量后需要执行策略
        executor.initialize();  //初始化创建该线程池
        return executor;
    }

}
